﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using DevExpress.XtraEditors;
using XCI.Core;

namespace XCI.Windows.Controls
{
    /// <summary>
    /// 属性控件
    /// </summary>
    [System.ComponentModel.DesignerCategory("Code")]
    public class PropertyControl : XtraUserControl
    {
        #region 属性

        /// <summary>
        /// 显示描述
        /// </summary>
        [DefaultValue(true), Description("显示描述")]
        public bool ShowDescription
        {
            get { return biDescription.Down; }
            set
            {
                biDescription.Down = value;
            }
        }

        /// <summary>
        /// 显示分类
        /// </summary>
        [DefaultValue(true), Description("显示分类")]
        public bool ShowCategories
        {
            get { return bciCategories.Checked; }
            set
            {
                if (value)
                    bciCategories.Checked = true;
                else bciAlphabetical.Checked = true;
            }
        }

        /// <summary>
        /// 显示按钮
        /// </summary>
        [DefaultValue(true), Description("显示按钮")]
        public bool ShowButtons
        {
            get { return bMain.Visible; }
            set
            {
                bMain.Visible = pnlTop.Visible = value;
            }
        }

        /// <summary>
        /// 属性表格
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public DevExpress.XtraVerticalGrid.PropertyGridControl PropertyGrid
        {
            get { return propertyGridControl1; }
        }

        #endregion

        /// <summary>
        /// 本地化字典
        /// </summary>
        public static readonly Dictionary<string, KeyValue> LocalDic = new Dictionary<string, KeyValue>();

        /// <summary>
        /// 构造函数
        /// </summary>
        public PropertyControl()
        {
            InitializeComponent();
            biDescription.Down = true;
            bciCategories.Checked = true;
            barManager1.Images = DevExpress.Utils.Controls.ImageHelper.
                    CreateImageCollectionCore(XCI.Windows.Properties.Resources.PropertyHead, 
                                              new Size(16, 16), Color.Transparent);

            if (!DesignMode)
            {
                InitLocalDic();
            }
        }

        /// <summary>
        /// 初始化本地字典
        /// </summary>
        private void InitLocalDic()
        {
            if (LocalDic.Count == 0)
            {
                LocalDic.Add("BackColor", new KeyValue("BackColor", "背景颜色"));
                LocalDic.Add("BackColor2", new KeyValue("BackColor2", "背景渐变颜色"));
                LocalDic.Add("BorderColor", new KeyValue("BorderColor", "边框颜色"));
                LocalDic.Add("Font", new KeyValue("Font", "字体"));
                LocalDic.Add("Bold", new KeyValue("Bold", "加粗"));
                LocalDic.Add("GdiCharSet", new KeyValue("GdiCharSet", "字符集"));
                LocalDic.Add("GdiVerticalFont", new KeyValue("GdiVerticalFont", "垂直字体"));
                LocalDic.Add("Italic", new KeyValue("Italic", "斜体"));
                LocalDic.Add("Name", new KeyValue("Name", "名称"));
                LocalDic.Add("Size", new KeyValue("Size", "大小"));
                LocalDic.Add("Strikeout", new KeyValue("Strikeout", "删除线"));
                LocalDic.Add("Underline", new KeyValue("Underline", "下划线"));
                LocalDic.Add("Unit", new KeyValue("Unit", "单位"));
                LocalDic.Add("ForeColor", new KeyValue("ForeColor", "字体颜色"));
                LocalDic.Add("GradientMode", new KeyValue("GradientMode", "背景渐变模式"));
                LocalDic.Add("Image", new KeyValue("Image", "背景图片"));
                LocalDic.Add("Options", new KeyValue("Options", "界面选项"));
                LocalDic.Add("UseBackColor", new KeyValue("UseBackColor", "使用背景色"));
                LocalDic.Add("UseBorderColor", new KeyValue("UseBorderColor", "使用边框色"));
                LocalDic.Add("UseFont", new KeyValue("UseFont", "使用字体"));
                LocalDic.Add("UseForeColor", new KeyValue("UseForeColor", "使用字体颜色"));
                LocalDic.Add("UseImage", new KeyValue("UseImage", "使用背景图片"));
                LocalDic.Add("UseTextOptions", new KeyValue("UseTextOptions", "使用文本选项"));
                LocalDic.Add("TextOptions", new KeyValue("TextOptions", "文本选项"));
                LocalDic.Add("HAlignment", new KeyValue("HAlignment", "水平对齐方式"));
                LocalDic.Add("HotkeyPrefix", new KeyValue("HotkeyPrefix", "热键前缀"));
                LocalDic.Add("Trimming", new KeyValue("Trimming", "整理方式"));
                LocalDic.Add("VAlignment", new KeyValue("VAlignment", "垂直对齐方式"));
                LocalDic.Add("WordWrap", new KeyValue("WordWrap", "是否换行"));

                //LocalDic.Add("Default", new KeyValue("Default", "默认"));
                //LocalDic.Add("Near", new KeyValue("Near", "左对齐"));
                //LocalDic.Add("Center", new KeyValue("Center", "居中"));
                //LocalDic.Add("Far", new KeyValue("Far", "右对齐"));


                //LocalDic.Add("Top", new KeyValue("Top", "顶端"));
                //LocalDic.Add("Bottom", new KeyValue("Bottom", "底部"));
            }
        }

        /// <summary>
        /// 选中行事件
        /// </summary>
        private void propertyGridControl1_FocusedRowChanged(object sender, DevExpress.XtraVerticalGrid.Events.FocusedRowChangedEventArgs e)
        {
            System.ComponentModel.PropertyDescriptor descriptor = null;
            if (e.Row != null) descriptor = propertyGridControl1.GetPropertyDescriptor(e.Row);
            if (descriptor != null)
            {
                string caption = descriptor.DisplayName;
                string desc = descriptor.Description;
                if (caption.Equals(desc))
                {
                    lbCaption.Text = caption;
                }
                else
                {
                    lbCaption.Text = caption + "<br>" + desc;
                }
            }
            else if (e.Row != null)
            {
                lbCaption.Text = e.Row.Properties.Caption;
            }
            else
            {
                lbCaption.Text = string.Empty;
            }
            SetPanelHeight();
        }

        /// <summary>
        /// 属性表格大小变化时
        /// </summary>
        private void XtraPropertyGrid_Resize(object sender, System.EventArgs e)
        {
            SetPanelHeight();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bci_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            propertyGridControl1.OptionsView.ShowRootCategories = bciCategories.Checked;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void biDescription_DownChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            pncDescription.Visible = pnlBottom.Visible = biDescription.Down;
        }

        /// <summary>
        /// 设置属性对象
        /// </summary>
        /// <param name="obj">属性对象</param>
        public void SetObject(object obj)
        {
            PropertyGrid.SelectedObject = obj;
        }

        /// <summary>
        /// 设置描述面板高度
        /// </summary>
        private void SetPanelHeight()
        {
            if (lbCaption.Text.Equals("(Name)"))
            {
                pncDescription.Height = 0;
            }
            else
            {
                pncDescription.Height = lbCaption.Height + 20;
            }
        }

        /// <summary>
        /// 自定义属性描述
        /// </summary>
        private void propertyGridControl1_CustomPropertyDescriptors(object sender, DevExpress.XtraVerticalGrid.Events.CustomPropertyDescriptorsEventArgs e)
        {
            // 最外层
            if (e.Context.PropertyDescriptor == null)
            {
                e.Properties = SetProperty(e.Properties);
            }

            //内层
            if (e.Context.PropertyDescriptor != null)
            {
                e.Properties = SetProperty(e.Properties);
            }
        }

        /// <summary>
        /// 翻译属性
        /// </summary>
        /// <param name="sourceCollection"></param>
        /// <returns></returns>
        PropertyDescriptorCollection SetProperty(PropertyDescriptorCollection sourceCollection)
        {
            PropertyDescriptorCollection filteredCollection = new PropertyDescriptorCollection(null);
            if (sourceCollection == null)
            {
                return filteredCollection;
            }
            foreach (System.ComponentModel.PropertyDescriptor item in sourceCollection)
            {
                var des = new PropertyDescriptorx(item);
                string name = item.Name;
                if (LocalDic.ContainsKey(name))
                {
                    var local = LocalDic[name];
                    des.SetDisplayName(local.Value);
                    des.SetDescription(local.Value);
                }
                filteredCollection.Add(des);
            }
            return filteredCollection.Sort(new[] { "Font", "ForeColor", "BackColor", "BorderColor", "BackColor2", "GradientMode", "Image" });
        }

        private DevExpress.XtraEditors.LabelControl lbCaption;

        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.barManager1 = new DevExpress.XtraBars.BarManager();
            this.bMain = new DevExpress.XtraBars.Bar();
            this.bciCategories = new DevExpress.XtraBars.BarCheckItem();
            this.bciAlphabetical = new DevExpress.XtraBars.BarCheckItem();
            this.biDescription = new DevExpress.XtraBars.BarButtonItem();
            this.barDockControlTop = new DevExpress.XtraBars.BarDockControl();
            this.barDockControlBottom = new DevExpress.XtraBars.BarDockControl();
            this.barDockControlLeft = new DevExpress.XtraBars.BarDockControl();
            this.barDockControlRight = new DevExpress.XtraBars.BarDockControl();
            this.pncDescription = new DevExpress.XtraEditors.PanelControl();
            this.lbCaption = new DevExpress.XtraEditors.LabelControl();
            this.propertyGridControl1 = new DevExpress.XtraVerticalGrid.PropertyGridControl();
            this.repositoryItemCheckEdit1 = new DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit();
            this.repositoryItemColorEdit1 = new DevExpress.XtraEditors.Repository.RepositoryItemColorEdit();
            this.pnlTop = new System.Windows.Forms.Panel();
            this.pnlBottom = new System.Windows.Forms.Panel();
            ((System.ComponentModel.ISupportInitialize)(this.barManager1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pncDescription)).BeginInit();
            this.pncDescription.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.propertyGridControl1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.repositoryItemCheckEdit1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.repositoryItemColorEdit1)).BeginInit();
            this.SuspendLayout();
            // 
            // barManager1
            // 
            this.barManager1.AllowCustomization = false;
            this.barManager1.AllowMoveBarOnToolbar = false;
            this.barManager1.AllowQuickCustomization = false;
            this.barManager1.AllowShowToolbarsPopup = false;
            this.barManager1.Bars.AddRange(new DevExpress.XtraBars.Bar[] {
            this.bMain});
            this.barManager1.DockControls.Add(this.barDockControlTop);
            this.barManager1.DockControls.Add(this.barDockControlBottom);
            this.barManager1.DockControls.Add(this.barDockControlLeft);
            this.barManager1.DockControls.Add(this.barDockControlRight);
            this.barManager1.Form = this;
            this.barManager1.Items.AddRange(new DevExpress.XtraBars.BarItem[] {
            this.bciCategories,
            this.bciAlphabetical,
            this.biDescription});
            this.barManager1.MaxItemId = 3;
            this.barManager1.UseAltKeyForMenu = false;
            this.barManager1.UseF10KeyForMenu = false;
            // 
            // bMain
            // 
            this.bMain.BarName = "Main";
            this.bMain.DockCol = 0;
            this.bMain.DockRow = 0;
            this.bMain.DockStyle = DevExpress.XtraBars.BarDockStyle.Top;
            this.bMain.LinksPersistInfo.AddRange(new DevExpress.XtraBars.LinkPersistInfo[] {
            new DevExpress.XtraBars.LinkPersistInfo(this.bciCategories),
            new DevExpress.XtraBars.LinkPersistInfo(this.bciAlphabetical),
            new DevExpress.XtraBars.LinkPersistInfo(this.biDescription, true)});
            this.bMain.OptionsBar.AllowDelete = true;
            this.bMain.OptionsBar.AllowQuickCustomization = false;
            this.bMain.OptionsBar.DrawDragBorder = false;
            this.bMain.OptionsBar.UseWholeRow = true;
            this.bMain.Text = "Main";
            // 
            // bciCategories
            // 
            this.bciCategories.GroupIndex = 1;
            this.bciCategories.Hint = "Categorized";
            this.bciCategories.Id = 0;
            this.bciCategories.ImageIndex = 0;
            this.bciCategories.Name = "bciCategories";
            this.bciCategories.CheckedChanged += new DevExpress.XtraBars.ItemClickEventHandler(this.bci_CheckedChanged);
            // 
            // bciAlphabetical
            // 
            this.bciAlphabetical.GroupIndex = 1;
            this.bciAlphabetical.Hint = "Alphabetic";
            this.bciAlphabetical.Id = 1;
            this.bciAlphabetical.ImageIndex = 1;
            this.bciAlphabetical.Name = "bciAlphabetical";
            this.bciAlphabetical.CheckedChanged += new DevExpress.XtraBars.ItemClickEventHandler(this.bci_CheckedChanged);
            // 
            // biDescription
            // 
            this.biDescription.ButtonStyle = DevExpress.XtraBars.BarButtonStyle.Check;
            this.biDescription.Hint = "Show Note";
            this.biDescription.Id = 2;
            this.biDescription.ImageIndex = 2;
            this.biDescription.Name = "biDescription";
            this.biDescription.DownChanged += new DevExpress.XtraBars.ItemClickEventHandler(this.biDescription_DownChanged);
            // 
            // barDockControlTop
            // 
            this.barDockControlTop.CausesValidation = false;
            this.barDockControlTop.Dock = System.Windows.Forms.DockStyle.Top;
            this.barDockControlTop.Location = new System.Drawing.Point(0, 0);
            this.barDockControlTop.Margin = new System.Windows.Forms.Padding(5, 5, 5, 5);
            this.barDockControlTop.Size = new System.Drawing.Size(261, 31);
            // 
            // barDockControlBottom
            // 
            this.barDockControlBottom.CausesValidation = false;
            this.barDockControlBottom.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.barDockControlBottom.Location = new System.Drawing.Point(0, 460);
            this.barDockControlBottom.Margin = new System.Windows.Forms.Padding(5, 5, 5, 5);
            this.barDockControlBottom.Size = new System.Drawing.Size(261, 0);
            // 
            // barDockControlLeft
            // 
            this.barDockControlLeft.CausesValidation = false;
            this.barDockControlLeft.Dock = System.Windows.Forms.DockStyle.Left;
            this.barDockControlLeft.Location = new System.Drawing.Point(0, 31);
            this.barDockControlLeft.Margin = new System.Windows.Forms.Padding(5, 5, 5, 5);
            this.barDockControlLeft.Size = new System.Drawing.Size(0, 429);
            // 
            // barDockControlRight
            // 
            this.barDockControlRight.CausesValidation = false;
            this.barDockControlRight.Dock = System.Windows.Forms.DockStyle.Right;
            this.barDockControlRight.Location = new System.Drawing.Point(261, 31);
            this.barDockControlRight.Margin = new System.Windows.Forms.Padding(5, 5, 5, 5);
            this.barDockControlRight.Size = new System.Drawing.Size(0, 429);
            // 
            // pncDescription
            // 
            this.pncDescription.Appearance.BackColor = System.Drawing.SystemColors.Info;
            this.pncDescription.Appearance.Options.UseBackColor = true;
            this.pncDescription.Controls.Add(this.lbCaption);
            this.pncDescription.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.pncDescription.Location = new System.Drawing.Point(0, 398);
            this.pncDescription.LookAndFeel.Style = DevExpress.LookAndFeel.LookAndFeelStyle.Flat;
            this.pncDescription.LookAndFeel.UseDefaultLookAndFeel = false;
            this.pncDescription.Margin = new System.Windows.Forms.Padding(5, 5, 5, 5);
            this.pncDescription.Name = "pncDescription";
            this.pncDescription.Size = new System.Drawing.Size(261, 62);
            this.pncDescription.TabIndex = 6;
            // 
            // lbCaption
            // 
            this.lbCaption.AllowHtmlString = true;
            this.lbCaption.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbCaption.Location = new System.Drawing.Point(3, 3);
            this.lbCaption.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
            this.lbCaption.Name = "lbCaption";
            this.lbCaption.Size = new System.Drawing.Size(45, 19);
            this.lbCaption.TabIndex = 0;
            this.lbCaption.Text = "(Name)";
            // 
            // propertyGridControl1
            // 
            this.propertyGridControl1.DefaultEditors.AddRange(new DevExpress.XtraVerticalGrid.Rows.DefaultEditor[] {
            new DevExpress.XtraVerticalGrid.Rows.DefaultEditor(typeof(bool), this.repositoryItemCheckEdit1),
            new DevExpress.XtraVerticalGrid.Rows.DefaultEditor(typeof(System.Drawing.Color), this.repositoryItemColorEdit1)});
            this.propertyGridControl1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.propertyGridControl1.Location = new System.Drawing.Point(0, 35);
            this.propertyGridControl1.Margin = new System.Windows.Forms.Padding(5, 5, 5, 5);
            this.propertyGridControl1.Name = "propertyGridControl1";
            this.propertyGridControl1.RepositoryItems.AddRange(new DevExpress.XtraEditors.Repository.RepositoryItem[] {
            this.repositoryItemCheckEdit1,
            this.repositoryItemColorEdit1});
            this.propertyGridControl1.Size = new System.Drawing.Size(261, 359);
            this.propertyGridControl1.TabIndex = 7;
            this.propertyGridControl1.CustomPropertyDescriptors += new DevExpress.XtraVerticalGrid.Events.CustomPropertyDescriptorsEventHandler(this.propertyGridControl1_CustomPropertyDescriptors);
            this.propertyGridControl1.FocusedRowChanged += new DevExpress.XtraVerticalGrid.Events.FocusedRowChangedEventHandler(this.propertyGridControl1_FocusedRowChanged);
            // 
            // repositoryItemCheckEdit1
            // 
            this.repositoryItemCheckEdit1.AutoHeight = false;
            this.repositoryItemCheckEdit1.Name = "repositoryItemCheckEdit1";
            // 
            // repositoryItemColorEdit1
            // 
            this.repositoryItemColorEdit1.AutoHeight = false;
            this.repositoryItemColorEdit1.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
            new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)});
            this.repositoryItemColorEdit1.Name = "repositoryItemColorEdit1";
            this.repositoryItemColorEdit1.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
            // 
            // pnlTop
            // 
            this.pnlTop.Dock = System.Windows.Forms.DockStyle.Top;
            this.pnlTop.Location = new System.Drawing.Point(0, 31);
            this.pnlTop.Margin = new System.Windows.Forms.Padding(5, 5, 5, 5);
            this.pnlTop.Name = "pnlTop";
            this.pnlTop.Size = new System.Drawing.Size(261, 4);
            this.pnlTop.TabIndex = 8;
            // 
            // pnlBottom
            // 
            this.pnlBottom.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.pnlBottom.Location = new System.Drawing.Point(0, 394);
            this.pnlBottom.Margin = new System.Windows.Forms.Padding(5, 5, 5, 5);
            this.pnlBottom.Name = "pnlBottom";
            this.pnlBottom.Size = new System.Drawing.Size(261, 4);
            this.pnlBottom.TabIndex = 9;
            // 
            // PropertyControl
            // 
            //this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 19F);
            //this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
            this.Controls.Add(this.propertyGridControl1);
            this.Controls.Add(this.pnlBottom);
            this.Controls.Add(this.pnlTop);
            this.Controls.Add(this.pncDescription);
            this.Controls.Add(this.barDockControlLeft);
            this.Controls.Add(this.barDockControlRight);
            this.Controls.Add(this.barDockControlBottom);
            this.Controls.Add(this.barDockControlTop);
            this.Margin = new System.Windows.Forms.Padding(5, 5, 5, 5);
            this.Name = "PropertyControl";
            this.Size = new System.Drawing.Size(261, 460);
            this.Resize += new System.EventHandler(this.XtraPropertyGrid_Resize);
            ((System.ComponentModel.ISupportInitialize)(this.barManager1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pncDescription)).EndInit();
            this.pncDescription.ResumeLayout(false);
            this.pncDescription.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.propertyGridControl1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.repositoryItemCheckEdit1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.repositoryItemColorEdit1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private DevExpress.XtraBars.BarManager barManager1;
        private DevExpress.XtraBars.BarDockControl barDockControlTop;
        private DevExpress.XtraBars.BarDockControl barDockControlBottom;
        private DevExpress.XtraBars.BarDockControl barDockControlLeft;
        private DevExpress.XtraBars.BarDockControl barDockControlRight;
        private DevExpress.XtraBars.Bar bMain;
        private DevExpress.XtraEditors.PanelControl pncDescription;
        private DevExpress.XtraVerticalGrid.PropertyGridControl propertyGridControl1;
        private System.Windows.Forms.Panel pnlTop;
        private System.Windows.Forms.Panel pnlBottom;
        private DevExpress.XtraBars.BarCheckItem bciCategories;
        private DevExpress.XtraBars.BarCheckItem bciAlphabetical;
        private DevExpress.XtraBars.BarButtonItem biDescription;
        private DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit repositoryItemCheckEdit1;
        private DevExpress.XtraEditors.Repository.RepositoryItemColorEdit repositoryItemColorEdit1;
    }
}
